package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.BalanceLog;
import com.jeequan.jeepay.service.BalanceLogService;
import com.jeequan.jeepay.service.mapper.BalanceLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;

import java.math.BigDecimal;
//import .entity.BalanceLog;
//import .mapper.BalanceLogMapper;
//import .service.BalanceLogService;
 /**
 * 账本日志表;(balance_log)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class BalanceLogServiceImpl implements BalanceLogService {
    @Autowired
    private BalanceLogMapper balanceLogMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public BalanceLog queryById(Integer id){
        return balanceLogMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param balanceLog 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<BalanceLog> paginQuery(BalanceLog balanceLog, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<BalanceLog> queryWrapper = new LambdaQueryWrapper<>();
        if(balanceLog.getMemberId()!=null){
            queryWrapper.eq(BalanceLog::getMemberId, balanceLog.getMemberId());
        }
        if(balanceLog.getBalanceType()!=null){
            queryWrapper.eq(BalanceLog::getBalanceType, balanceLog.getBalanceType());
        }
        if(balanceLog.getState()!=null){
            queryWrapper.eq(BalanceLog::getState, balanceLog.getState());
        }
        if(StrUtil.isNotBlank(balanceLog.getRemark())){
            queryWrapper.eq(BalanceLog::getRemark, balanceLog.getRemark());
        }
        if(StrUtil.isNotBlank(balanceLog.getBonusFrom())){
            queryWrapper.eq(BalanceLog::getBonusFrom, balanceLog.getBonusFrom());
        }
        //2. 执行分页查询
        Page<BalanceLog> pagin = new Page<>(current , size , true);
        IPage<BalanceLog> selectResult = balanceLogMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param balanceLog 实例对象
     * @return 实例对象
     */
    public BalanceLog insert(BalanceLog balanceLog){
        balanceLogMapper.insert(balanceLog);
        return balanceLog;
    }
    
    /** 
     * 更新数据
     *
     * @param balanceLog 实例对象
     * @return 实例对象
     */
    public BalanceLog update(BalanceLog balanceLog){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<BalanceLog> chainWrapper = new LambdaUpdateChainWrapper<BalanceLog>(balanceLogMapper);
        if(StrUtil.isNotBlank(balanceLog.getRemark())){
            chainWrapper.set(BalanceLog::getRemark, balanceLog.getRemark());
        }
        if(StrUtil.isNotBlank(balanceLog.getBonusFrom())){
            chainWrapper.set(BalanceLog::getBonusFrom, balanceLog.getBonusFrom());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(BalanceLog::getId, balanceLog.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(balanceLog.getId());
        }else{
            return balanceLog;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = balanceLogMapper.deleteById(id);
        return total > 0;
    }

     @Override
     public void addBalanceLog(Integer memberId, Integer balanceType, Integer balanceLevel, BigDecimal balance, BigDecimal oldBalance, BigDecimal newBalance, Integer sourceType, Integer sourceId, Integer operateType) {
        BalanceLog balanceLog = new BalanceLog();
        balanceLog.setMemberId(memberId);
        balanceLog.setBalanceType(balanceType);
        balanceLog.setBalanceLevel(balanceLevel);
        balanceLog.setBalance(balance);
        balanceLog.setOldBalance(oldBalance);
        balanceLog.setNewBalance(newBalance);
        balanceLog.setSourceType(sourceType);
        balanceLog.setLogType(operateType);
        balanceLog.setSourceId(sourceId);
     }
 }